<template>
  <el-form :model="empForm" :rules="rules2" ref="empForm" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">系统登录</h3>
    <el-form-item prop="account">账号
      <el-input type="text" v-model="empForm.account" auto-complete="off" placeholder="请输入账号"></el-input>
    </el-form-item>
<!--    <el-form-item prop="phone">电话
    <el-input type="text" v-model="empForm.phone" auto-complete="off" placeholder="请输入电话"></el-input>
  </el-form-item>-->
    <el-form-item prop="checkPass">密码
      <el-input type="password" v-model="empForm.password" auto-complete="off" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:47%;" @click.native.prevent="loginSubmit" :loading="logining">登录</el-button>
      <el-button type="primary" style="width:47%;" @click.native.prevent="goRegister"> 店铺入驻 </el-button>
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>

  </el-form>
</template>

<script>
  export default {
    data() {
      return {
        logining: false,
        empForm: {
          account: '东邪',
          password: '1',
          type:0,
        },
        rules2: {
          account: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            //{ validator: validaePass }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            //{ validator: validaePass2 }
          ],
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            //{ validator: validaePass2 }
          ]
        },
        checked: true
      };
    },
    methods: {
      goRegister() {
        //要跳转到路由的路径 - 之间登录成功之后的跳转代码
        this.$router.push({ path: '/register' });
      },
      handleReset2() {
        this.$refs.empForm.resetFields();
      },
      // 后台管理员登录操作
      loginSubmit(ev) {
        this.$refs.empForm.validate((valid) => {
          if (valid) {
            this.logining = true; // 开启忙等框
            this.$http.post("/login/account",this.empForm).then(res=>{
              this.logining = false;// 关闭忙等框
              if (res.data.success){
                // 获取token 存入localstoage
                var token = res.data.resultObj.token;
                var logininfo = res.data.resultObj.logininfo;
                var menus = res.data.resultObj.menus;
                var permissions = res.data.resultObj.permissions;

                localStorage.setItem("token",token);
                localStorage.setItem("logininfo",JSON.stringify(logininfo));
                localStorage.setItem("menus",JSON.stringify(menus));
                localStorage.setItem("permissions",JSON.stringify(permissions));

                // 跳转到页面
                this.$router.push({ path: '/echarts' });
                // 在login.vue页面登录成功之后刷新一下本地的路由缓存
                location.reload();
              }else{
                this.$message({
                  message: res.data.message,
                  type: 'error'
                });
              }
            }).catch(res=>{
              this.logining = false;// 关闭忙等框
              this.$message({
                message: res.data.message,
                type: 'error'
              });
              })
          } else {
            this.$message({
              message: "参数异常",
              type: 'error'
            });
            return false;
          }
        });
      }
    }
  }

</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
  }
</style>